package com.dd.cloud.user.mq;

import com.dd.cloud.user.entity.log.LogException;
import com.dd.cloud.user.entity.log.LogOper;
import com.dd.cloud.user.service.log.ILogExceptionService;
import com.dd.cloud.user.service.log.ILogOperService;
import com.dd.cloud.user.service.news.IAppPromotionNewsService;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 系统消息 队列消费者 延时队列
 *
 * @author zk
 */
@Slf4j
@Component
public class ReceiverNoticeMessage {
    @Autowired
    private ILogOperService logOperService;
    @Autowired
    private ILogExceptionService logExceptionService;
    @Autowired
    private IPresPrescriptionService presPrescriptionService;
    @Autowired
    private IAppPromotionNewsService appPromotionNewsService;

    /**
     * 延时队列统一消费
     *
     * @param channel
     * @param message
     * @param logOper
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = MQConfig.LOG_OPER_QUERE)
    public void processHandler(Channel channel, Message message, LogOper logOper) throws IOException {
        try {
            log.info("操作日志收到消息：{}", logOper);
            logOperService.addLog(logOper);
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

    /**
     * 系统消息队列消费者
     *
     * @param channel
     * @param message
     * @param logException
     * @throws IOException
     */
    @RabbitHandler
    @RabbitListener(queues = MQConfig.LOG_EXC_QUERE)
    public void processHandler(Channel channel, Message message, LogException logException) throws IOException {
        try {
            log.info("异常消息：{}", logException);
            logExceptionService.addLogExc(logException);
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

    @RabbitHandler
    @RabbitListener(queues = MQConfig.PRES_PDF_QUERE)
    public void processHandler(Channel channel, Message message, String tencode) throws IOException {
        try {
            log.info("生成处方电子单tencode：{}", tencode);
            presPrescriptionService.generatePresPdf(tencode);
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

    /**
     * @param channel:
     * @param message:
     * @param id:
     * @return void
     * @description app消息消费者
     * @author czg
     * @date 2023/9/18 15:41
     */
    @RabbitHandler
    @RabbitListener(queues = MQConfig.APP_NEWS_QUEUE)
    public void processHandler(Channel channel, Message message, Integer id) throws IOException {
        try {
            log.info("队列消费者->app发送消息：{}", id);
            appPromotionNewsService.reSendAppPromotionNews(id, 2);
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }
}
